/*
 * PointBuffResource.cpp
 *
 *  Created on: 2013. 9. 6.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "PointBuffResource.h"
#include "../NUtil/Utils.h"
#include "../engine.h"

namespace ProjectZ { namespace NResource {

PointBuffResource::PointBuffResource()
{
	pPointBuffAndroid	= new PointBuffData;
	pPointBuffIOS		= new PointBuffData;
}

PointBuffResource::~PointBuffResource()
{
	delete pPointBuffAndroid;
	delete pPointBuffIOS;
}

bool PointBuffResource::LoadResource()
{
	if ( false == pPointBuffAndroid->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE) )
		return false;

	if ( false == pPointBuffIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS) )
		return false;

	return true;
}

PointBuffData::Flyweight * PointBuffResource::GetFly(int company, int saleCode, int buffSeq)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pPointBuffAndroid->GetFly(buffSeq);

	case NUtil::MarketType::IOS_KAKAO:
		return pPointBuffIOS->GetFly(buffSeq);

	default:
		return NULL;
	}

	return NULL;
}


PointBuffData::~PointBuffData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

bool PointBuffData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_point_buff WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int tmp_company;
		int tmp_salecode;

		PointBuffData::Flyweight * pFly = new PointBuffData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> tmp_company >> tmp_salecode >> pFly->_BUFF_SEQ >> pFly->_MINUTE >> pFly->_RATE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_BUFF_SEQ] = pFly;
	}

	return true;
}

PointBuffData::Flyweight * PointBuffData::GetFly(int buffSeq)
{
	auto iter = clsFlyTree.find(buffSeq);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

} /* NResource */ } /* ProjectZ */
